package com.hr.aop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Repository
public class LoginInterceptor extends HandlerInterceptorAdapter {
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		// 后台session控制
		String[] noFilters = new String[] { "login.jsp", "/getImage.html" };
		String uri = request.getRequestURI();

		boolean beFilter = true;
		for (String s : noFilters) {
			if (uri.indexOf(s) != -1) {
				beFilter = false;
				break;
			}
			if (beFilter) {
				Object obj = request.getSession().getAttribute("user");
				if (null == obj) {
					response.sendRedirect("login.jsp");
					return false;
				}
			}
		}

		return super.preHandle(request, response, handler);
	}
}
